package io.sunny.platform.ai.deepseek.session;

import io.github.pigmesh.ai.deepseek.core.chat.Message;
import io.github.pigmesh.ai.deepseek.core.chat.Role;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * @Author SUNNY
 * @Date 2025/11/29 22:56
 * @PackageName: io.sunny.platform.ai.deepseek.session
 * @Description: TODO
 * @Version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DialogSession {
    private String id;
    private List<Message> messages;
    private long lastUpdateTime;

    public DialogSession(String id, List<Message> messages) {
        this.id = id;
        this.messages = messages;
        this.lastUpdateTime = System.currentTimeMillis();
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Message implements io.github.pigmesh.ai.deepseek.core.chat.Message {
        private String role;
        private String content;
        private Long currentTime;

        @Override
        public Role role() {
            return null;
        }
    }

    public String getRecentContent() {
        StringBuilder str = new StringBuilder();
        messages.forEach(message -> str.append(message.role).append(": ").append(message.getContent()).append("\n"));
        return str.toString();
    }

    public int getMessageCount() {
        return messages.size();
    }

    /**
     * 保留最近的一半记录
     */
    public void trimMessages(int half) {
        messages = messages.subList(messages.size() - half, messages.size());
    }

    public void addMessage(Message message) {
        messages.add(message);
    }
}
